package com.xxx.controller;

import com.xxx.service.IKillCourseService;
import com.xxx.domain.KillCourse;
import com.xxx.query.KillCourseQuery;
import com.xxx.result.JSONResult;
import com.xxx.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/killCourse" )
public class KillCourseController {


    @Autowired
    public IKillCourseService killCourseService;

    /**
     *秒杀课程
     */
    @RequestMapping(value = "/kill/{activityId}/{courseId}", method = RequestMethod.POST)
    public JSONResult kill(@PathVariable("activityId") Long activityId,
                           @PathVariable("courseId") Long courseId) {

        return JSONResult.success(killCourseService.kill(activityId,courseId));
    }

    /**
     * 获取某个秒杀课程
     */

    @RequestMapping(value = "/online/one/{activityId}/{courseId}", method = RequestMethod.GET)
    public JSONResult onlineOne(@PathVariable("activityId") Long activityId,@PathVariable("courseId") Long courseId) {
        return JSONResult.success(killCourseService.onlineOne(activityId,courseId));
    }


    /**
     * 获取所有已经发布的秒杀课程
     */

    @RequestMapping(value = "/online/all", method = RequestMethod.GET)
    public JSONResult onlineAll() {
        return JSONResult.success(killCourseService.onlineAll());
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse) {
        if (killCourse.getId() != null){
                killCourseService.updateById(killCourse);
        }else{
                killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id" ) Long id) {
            killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id" ) Long id) {
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query) {
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(), query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(), page.getRecords()));
    }
}
